package com.coocaa.ops.admin.v1.homepage.provider.impl;

import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageInfoBizImpl;
import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageModuleRlsBizImpl;
import com.coocaa.ops.admin.v1.homepage.convert.HomeQueryConverter;
import com.coocaa.ops.admin.v1.homepage.mapper.IHomepageInfoMapper;
import com.coocaa.ops.admin.v1.homepage.model.HomepageInfoModel;
import com.coocaa.ops.admin.v1.homepage.model.query.HomeModuleQueryModel;
import com.coocaa.ops.admin.v1.homepage.provider.IHomepageInfoQueryProvider;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;

import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupChipModelConfigEntity;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author bjh
 * @description 首页信息原始表-查询provider接口实现
 * @date : 2020-06-15 16:31:15
 */
@Service
public class HomepageInfoQueryProviderImpl implements IHomepageInfoQueryProvider{
    @Autowired
    private HomepageInfoBizImpl thisBizImpl;
    @Autowired
    private HomepageModuleRlsBizImpl homepageModuleRlsBiz;
    @Autowired
    private IHomepageInfoMapper homepageInfoMapper;


    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Override
    public HomepageInfoEntity getDetailInfo(Integer id, Integer version) {
        HomepageInfoEntity entity = this.getEntityByIdAndVersion(id, version);
        HomeModuleQueryModel model = new HomeModuleQueryModel(entity.getId(), entity.getCurrentVersion());
        List<HomeModuleContentConfigInfo> commonModuleList = homepageModuleRlsBiz.getMapper().queryCommonModuleList(model);
        List<HomeModuleContentConfigInfo> dmpModuleList = homepageModuleRlsBiz.getMapper().queryDmpModuleList(model);
        entity.setModuleList(HomeQueryConverter.convertFinalModuleList(commonModuleList, dmpModuleList));
        return entity;
    }

    /**
     * 分页数据-菜单自己使用
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<HomepageInfoEntity> pageList(HomepageInfoModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<HomepageInfoEntity> pageResult = thisBizImpl.getCommonPageList(params, true);
        this.handleQueryRlsForPageData(pageResult.getResultList());
        return pageResult;
    }

    /**
     * 分页数据-其他处选择
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<HomepageInfoEntity> dataList(HomepageInfoModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<HomepageInfoEntity> pageResult = thisBizImpl.getCommonPageList(params, false);
        this.handleQueryRlsForPageData(pageResult.getResultList());
        return pageResult;

    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private void handleQueryRlsForPageData(List<HomepageInfoEntity> homepageInfoEntityList){
        for (HomepageInfoEntity entity : homepageInfoEntityList){
             List<PolicyGroupInfoEntity> rlsPolicyList = homepageInfoMapper.queryRlsPolicy(entity.getId());
            HomeQueryConverter.handleSetRlsPolicyName(rlsPolicyList, entity);
             List<PolicyGroupChipModelConfigEntity> rlsChipModelList = homepageInfoMapper.queryRlsChipModel(entity.getId());
            HomeQueryConverter.handleSetRlsChiModelName(rlsChipModelList, entity);

        }
    }

    /**
     * 获取基础信息-根据ID和版本
     *
     * @param id
     * @param version
     * @return
     */
    private HomepageInfoEntity getEntityByIdAndVersion(Integer id, Integer version) {
        return thisBizImpl.getEntityByIdAndVersion(id, version);
    }


}
